from abc import ABC, abstractmethod

from langchain_core.language_models import BaseChatModel
from pydantic import BaseModel, Field


class PlatformInfo(BaseModel):
    platform_name: str = Field(..., description="平台名称")
    support_model_list: list[str] = Field(..., description="支持的模型列表")


class BasePlatformChatModel(ABC):
    PLATFORM_NAME: str
    SUPPORT_MODEL_LIST: list[str]
    MODEL_INSTANCE_MAP: dict[str, BaseChatModel]

    def get_platform_info(self) -> PlatformInfo:
        return PlatformInfo(**{
            "platform_name": self.PLATFORM_NAME,
            "support_model_list": self.SUPPORT_MODEL_LIST
        })

    def get_support_model_list(self) -> list[str]:
        return self.SUPPORT_MODEL_LIST

    def get_model_ins_by_name(self, model_name: str) -> BaseChatModel:
        ins = self.MODEL_INSTANCE_MAP.get(model_name, None)
        if not ins:
            raise Exception(f"not register model {model_name}")
        return ins

    @abstractmethod
    def init_model(self, *model_names: str, **options: object) -> "BasePlatformChatModel":
        if not model_names:
            raise Exception(f"Illegal model name {model_names}")
        for model_name in model_names:
            if model_name not in self.SUPPORT_MODEL_LIST:
                raise Exception(f"{self.PLATFORM_NAME} platform no support models: {model_name}")
        return self
